﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using Enterprise.Infrastructure;
using Enterprise.Model.Note;
using Enterprise.Service.Note;
namespace Enterprise.UI.Web.Modules.Notice
{
    /// <summary>
    /// Notice 的摘要说明
    /// </summary>
    public class Notice : IHttpHandler
    {
        int year = (int)Utility.sink("year", Utility.MethodType.Get, 0, 0, Utility.DataType.Int);
        int month = (int)Utility.sink("month", Utility.MethodType.Get, 0, 0, Utility.DataType.Int);
        string action = (string)Utility.sink("action", Utility.MethodType.Get, 0, 0, Utility.DataType.Str);
        public void ProcessRequest(HttpContext context)
        {
            if (year == 0 || month == 0)
            {
                year = DateTime.Now.Year;
                month = DateTime.Now.Month;
            }
            else
            {
                switch (action)
                {
                    case "pre":
                        if (month == 1)
                        {
                            year = year - 1;
                            month = 12;
                        }
                        else
                        {
                            month = month - 1;
                        }
                        break;
                    case "next":
                        if (month == 12)
                        {
                            year = year + 1;
                            month = 1;
                        }
                        else
                        {
                            month = month + 1;
                        }
                        break;
                }
            }
            context.Response.ContentType = "text/plain";
            StringBuilder sb = new StringBuilder();
            sb.Append(CreateCalendar());
            context.Response.Write(sb.ToString());
        }

        private string CreateCalendar()
        {
            int days = DateTime.DaysInMonth(year, month);//得到当月的天数  
            DateTime dt = new DateTime(year, month, 1);
            int weeke = Convert.ToInt32(dt.DayOfWeek.ToString("d"));//得到当月的第一天是星期几  
            string[,] cal = new string[7, 6];//创建一个二维数组来存放日期  
            for (int day = 1; day <= days; day++)
            {
                int x = (day + weeke - 1) % 7;//根据星期的规律循环x轴坐标  
                int y = (day + weeke - 1) / 7;//根据（月/星期）循环Y轴坐标  
                for (int j = 0; j < 6; j++)
                {
                    for (int i = 0; i < 7; i++)
                    {
                        cal[x, y] = day.ToString();//开始赋值  
                    }
                }
            }
            //输出  
            NoteService nService = new NoteService();
            System.Text.StringBuilder st = new System.Text.StringBuilder();
            st.Append("<table cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" height=\"200px\" class=\"calendar\">");
            st.Append("<tr><th class=\"first\"> 日</th><th>一</th><th>二</th><th>三</th><th>四</th><th>五</th><th>六</th></tr>");
            for (int i = 0; i < 6; i++)
            {
                st.Append("<tr>");
                for (int j = 0; j < 7; j++)
                {
                    string day = cal[j, i];
                    if (j == 0)
                    {
                        st.Append("<td class=\"first\"");
                    }
                    else
                    {
                        st.Append("<td ");
                    }
                    if (!string.IsNullOrEmpty(day))
                    {
                        DateTime time = Convert.ToDateTime(year + "/" + month + "/" + day);
                        var list = nService.NoteList().Where(p => p.NUSERID == Utility.Get_UserId && Convert.ToDateTime(p.NCREATETIME.ToShortDateString()) == time).ToList();
                        if (list.Count > 0)
                        {
                            st.Append("style=\"background-image:url('/Styles/images/cbg.gif');background-repeat:no-repeat;background-position:center;\"");
                        }
                        st.Append("width=\"13%\" align=\"left\" id=\"td" + i + j + "\">");
                        st.Append("<span onclick='opdg(\"个人备忘\",\"/Modules/Notice/Notices.aspx?dt=" + time.ToShortDateString() + "\")' style=\"cursor:pointer;\">" + day + "</span></td>");
                    }
                    else
                    {
                        st.Append("></td>");
                    }                    
                }
                st.Append("</tr>");
            }
            st.Append("</table>");
            return st.ToString();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}